package com.clstu.preparedstatement;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class PrepareStatement02 {
    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url,user,password);
        String sql = "insert into us values(?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < 5; i++) {//插入5条数据
            String name = "tom"+i;
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,"123456");
            preparedStatement.executeUpdate();
        }
        String sql1 = "update us set name = 'king' where name = 'tom0'";
        preparedStatement.executeUpdate(sql1);//将某个tom0的名字改为king
        String sql2 = "delete from us where name = 'tom2'";
        preparedStatement.executeUpdate(sql2);//删除一个tom2
        String sql3="select * from us ";
        ResultSet resultSet = preparedStatement.executeQuery(sql3);//查询所有结果并显示
        while (resultSet.next()){
            System.out.println(resultSet.getString(1)+"\t"+resultSet.getString(2));
        }
        //关闭资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
